<template>
  <div class="menu-management-container">
    <el-row :gutter="20">
      <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
        <vab-query-form>
          <vab-query-form-top-panel :span="12">
            <el-button :icon="Plus" type="primary" @click="handleEdit()">
              添加
            </el-button>
          </vab-query-form-top-panel>
        </vab-query-form>
        <el-table
          v-loading="listLoading"
          border
          :data="list"
          default-expand-all
          row-key="path"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column label="标题" prop="name" show-overflow-tooltip />
          <el-table-column label="id" prop="id" align="center" width="100" />
          <el-table-column
            label="类型"
            prop="type"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="路径"
            prop="path"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="vue文件路径"
            prop="component"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column label="重定向" show-overflow-tooltip align="center">
            <template #default="{ row }">
              {{ row.redirect || '无' }}
            </template>
          </el-table-column>
          <el-table-column label="图标" align="center" show-overflow-tooltip />
          <el-table-column
            label="操作"
            align="center"
            show-overflow-tooltip
            width="200"
          >
            <template #default="{ row }">
              <el-button text type="primary" @click="handleEdit(row)">
                编辑
              </el-button>
              <el-button text type="primary" @click="handleDelete(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
          <template #empty>
            <el-empty class="vab-data-empty" description="暂无数据" />
          </template>
        </el-table>
      </el-col>
    </el-row>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, details, getList } from '@/api/menuManagement'
  import { Plus } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'MenuManagement',
    components: {
      Edit: defineAsyncComponent(() =>
        import('./components/MenuManagementEdit')
      ),
    },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        list: [],
        listLoading: true,
      })

      const handleEdit = async (row) => {
        if (row) {
          try {
            const { data: data } = await details(row.id)
            state['editRef'].showEdit(data)
          } catch (e) {
            console.log(e)
          }
        } else {
          state['editRef'].showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          try {
            $baseConfirm('你确定要删除当前项吗', null, async () => {
              const { id } = row
              const { msg } = await doDelete({ id: id })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } catch (e) {
            console.error(e)
          }
        }
      }
      const fetchData = async () => {
        try {
          state.listLoading = true
          const { data: data } = await getList({})
          state.list = data
          state.listLoading = false
        } catch (e) {
          console.log(e)
        }
      }

      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        handleEdit,
        handleDelete,
        fetchData,
        Plus,
      }
    },
  })
</script>

<style lang="scss" scoped>
  $base: '.menu-management';
  #{$base}-container {
    padding: 0 !important;
    background: $base-color-background !important;
  }
</style>
